import {
  $http
} from '@escook/request-miniprogram'
import store from './store/store.js'
// #ifdef MP-WEIXIN
$http.baseUrl = 'https://www.uinav.com'
// #endif
$http.beforeRequest = function(options) {
  uni.showLoading({
    title: '加载中...'
  })
// 判断请求的是否为有权限的 API 接口
  if (options.url.indexOf('/my/') !== -1) {
    // 为请求头添加身份认证字段
    options.header = {
      // 字段的值可以直接从 vuex 中进行获取
      Authorization: store.state.m_user.token,
    }
    // console.log(options);
  }
}
$http.afterRequest = function() {
  uni.hideLoading()
}
// 模拟数据
$http._post = $http.post
$http.post = function(url, data) {
  //做自己的处理
  if (url === '/api/public/v1/users/wxlogin') {
    return new Promise((resolve, reject) => {
      resolve({
        data: {
          "message": {
            "user_id": 56,
            "user_email_code": null,
            "is_active": null,
            "user_sex": "男",
            "user_qq": "",
            "user_tel": "",
            "user_xueli": "本科",
            "user_hobby": "",
            "user_introduce": null,
            "create_time": 1630138976,
            "update_time": 1630138976,
            "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjU2LCJpYXQiOjE2MzgwNzk2NDUsImV4cCI6MTAwMTYzODA3OTY0NH0.FvyRV1ccyHpAcogH-hzYu-7IKQbEwM1LQnVb2YZM3x8"
          },
          "meta": {
            "msg": "登录成功",
            "status": 200
          }
        }
      })
    })
  }
  return uni.$http._post(url, data)
}
export default $http
